<?php

/*
 * Created by: Dikkehein <Clemens Wijnekus, c.wijnekus@gmail.com>
 */

class Actiepunten extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('Ap_model');
        $this->load->model('Kampjaar_model');
        $this->load->model('Gebruiker_model');
    }

    public function index() {
        if ($this->session->userdata('logged_in') == TRUE) {
            $data['gebruiker'] = $this->session->all_userdata();
            $data['actiepunten'] = $this->Ap_model->alle_actiepunten_voor_kampjaar($data['gebruiker']['kampjaar']);
            $data['title'] = 'Actiepunten';
            $this->load->view('partial/header', $data);
            $this->load->view('partial/menu', $data);
            $this->load->view('partial/sidebar', $data);
            $this->load->view('actiepunten/index', $data);
        } else {
            redirect('auth');
        }
    }

    public function aanmaken() {
        if ($this->session->userdata('logged_in') == TRUE) {

            // Load support assets
            $this->form_validation->set_error_delimiters('', '<br />');

            // Set validation rules
            $this->form_validation->set_rules('beschrijving', 'Beschrijving', 'required|min_length[1]|max_length[125]');
            $this->form_validation->set_rules('ap_staffers[]', 'Staffers', 'required|min_length[1]|max_length[125]');
            $this->form_validation->set_rules('deadline', 'Deadline', 'required|min_length[10]|max_length[10]|alpha_dash');
            $this->form_validation->set_rules('jaar', 'Kampjaar', 'required|min_length[1]|max_length[4]|integer|is_natural');

            // Begin validation
            if ($this->form_validation->run() == FALSE) {
                // First load, or problem with form
                $data['beschrijving'] = array(
                    'name' => 'beschrijving',
                    'id' => 'beschrijving',
                    'value' => set_value('beschrijving', ''),
                    'maxlength' => '100',
                    'size' => '35');

                $data['ap_staffers[]'] = array(
                    'name' => 'ap_staffers[]',
                    'id' => 'ap_staffers[]',
                    'checked' => false);

                $data['deadline'] = array(
                    'name' => 'deadline',
                    'id' => 'deadline',
                    'value' => set_value('deadline', ''),
                    'maxlength' => '100',
                    'size' => '35');



                $data['gebruiker'] = $this->session->all_userdata();
                $data['staf'] = $this->Gebruiker_model->gebruikers_kampjaar($data['gebruiker']['kampjaar']);
                $data['title'] = 'Actiepunt aanmaken';
                $this->load->view('partial/header', $data);
                $this->load->view('partial/menu', $data);
                $this->load->view('partial/sidebar', $data);
                $this->load->view('actiepunten/aanmaken', $data);
            } else {
                $data = array(
                    'beschrijving' => $this->input->post('beschrijving'),
                    'deadline' => $this->input->post('deadline'),
                    'kampjaar' => $this->input->post('jaar')
                );
                $staffers = $this->input->post('ap_staffers');
                $id_ap = $this->Ap_model->aanmaken($data);
                if ($id_ap > 0) {
                    foreach ($staffers as $staf) :
                        $ap_g_data = array(
                            'id_gebruiker' => $staf,
                            'id_ap' => $id_ap
                        );

                        $this->Ap_model->koppel($ap_g_data);
                    endforeach;
                    redirect('actiepunten/aanmaken');
                }
            }
        } else {
            redirect('auth');
        }
    }
    
    public function check($id){
        if ($this->session->userdata('logged_in') == TRUE) {
            if($this->Ap_model->checkAp($id))
            {
                redirect ('actiepunten');
            }
        } else {
            redirect('auth');
        }
    }
    
    public function uncheck($id){
        if ($this->session->userdata('logged_in') == TRUE) {
            if($this->Ap_model->uncheckAp($id))
            {
                redirect ('actiepunten');
            }
        } else {
            redirect('auth');
        }
    }
    
    public function checkFromIndex($id){
        if ($this->session->userdata('logged_in') == TRUE) {
            if($this->Ap_model->checkAp($id))
            {
                redirect ('home');
            }
        } else {
            redirect('auth');
        }
    }
    
    public function printversie(){
        if ($this->session->userdata('logged_in') == TRUE) {
            $data['gebruiker'] = $this->session->all_userdata();
            $data['actiepunten'] = $this->Ap_model->alle_actiepunten_voor_kampjaar($data['gebruiker']['kampjaar']);
            $this->load->view('actiepunten/printversie', $data);
        } else {
            redirect('auth');
        }
        
    }
    

}
